<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        body{
            background-color: #222;
        }

        div {
            position: absolute;
            left: 50%;
            top: 50%;
            background: url(./images/tankT.png) no-repeat;
            background-size: cover;
            transform: translate(-50px, -50px);
            width: 100px;
            height: 100px;
        }

        p {
            width: 20px;
            height: 20px;
            position: absolute;
            background-repeat: no-repeat;
            background-size: cover;
        }
    </style>
</head>

<body>
    <div></div>
</body>
<script>
    var dv = document.querySelector('div');
    var kuan = window.innerWidth;
    var gao = window.innerHeight;
    var x = y = -50;
    var positi = 0;
    document.addEventListener('keydown', function (e) {
        if (e.keyCode == 87) {
            positi = e.keyCode;
            if (y <= -gao / 2) {
                return false
            } else {
                dv.style.backgroundImage = `url(./images/tankT.png)`
                dv.style.transform = `translate(${x}px,${y -= 5}px)`
            }
        } else if (e.keyCode == 65) {
            positi = e.keyCode;
            if (x <= -kuan / 2) {
                return false
            } else {
                dv.style.backgroundImage = `url(./images/tankL.png)`
                dv.style.transform = `translate(${x -= 5}px,${y}px)`
            }
        } else if (e.keyCode == 83) {
            positi = e.keyCode;
            if (y >= gao / 2 - 100) {
                return false
            } else {
                dv.style.backgroundImage = `url(./images/tankB.png)`
                dv.style.transform = `translate(${x}px,${y += 5}px)`
            }
        } else if (e.keyCode == 68) {
            positi = e.keyCode;
            if (x >= kuan / 2 - 100) {
                return false
            } else {
                dv.style.backgroundImage = `url(./images/tankR.png)`
                dv.style.transform = `translate(${x += 5}px,${y}px)`
            }
        }
        else if (e.keyCode == 13) {
            var zidan = document.createElement('p');
            dv.appendChild(zidan);
            if (positi == 87) {
                zidan.style.left = '40px';
                zidan.style.top = '-20px';
                zidan.style.backgroundImage = 'url(./images/zidanT.png)'
                animate(zidan, -120);
            } else if (positi == 65) {
                zidan.style.backgroundImage = 'url(./images/zidanL.png)'
                zidan.style.left = '-20px'
                zidan.style.top = '37px'
                animate(zidan, -130)
            }
            else if (positi == 83) {
                zidan.style.backgroundImage = 'url(./images/zidanB.png)'
                zidan.style.left = '39px'
                zidan.style.top = '100px'
                animate2(zidan, 220)
            } else if (positi == 68) {
                zidan.style.backgroundImage = 'url(./images/zidanR.png)'
                zidan.style.left = '100px'
                zidan.style.top = '37px'
                animate2(zidan, 230)
            }
        }
    })

    function animate(obj, late) {
        clearInterval(obj.times)
        obj.times = setInterval(function () {
            if (obj.offsetTop <= late) {
                obj.remove();
                clearInterval(obj.times);
            } else if (obj.offsetLeft <= late) {
                obj.remove();
                clearInterval(obj.times);
            }
            if (late == -120) {
                obj.style.top = obj.offsetTop - 2 + 'px';
            } else if (late == -130) {
                obj.style.left = obj.offsetLeft - 2 + 'px';
            }

        }, 30)
    }
    function animate2(obj, late) {
        clearInterval(obj.times)
        obj.times = setInterval(function () {
            if (obj.offsetTop >= late) {
                obj.remove();
                clearInterval(obj.times);
            } else if (obj.offsetLeft >= late) {
                obj.remove();
                clearInterval(obj.times);
            }
            if (late == 220) {
                obj.style.top = obj.offsetTop + 2 + 'px';
            } else if (late == 230) {
                obj.style.left = obj.offsetLeft + 2 + 'px';
            }

        }, 30)
    }
</script>

</html>